package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

/**
 * 反射机制调用有参方法
 */
public class ReflectDemo5 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person p = new Person();
        p.say("你好");

        Class cls = Class.forName("reflect.Person");
        Object obj = cls.newInstance();
        //say(String)
        //参数1是方法名，从参数2开始是方法的参数列表
        Method method = cls.getMethod("say", String.class);
        //参数1是方法所属对象，参数2开始是实参列表
        method.invoke(obj,"大家好");//obj.say("大家好")

        //say(String，int)
        Method method1 = cls.getMethod("say", String.class,int.class);
        method1.invoke(obj,"才是真的好",5);


    }
}
